﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace UserAccountsManager
{
    public partial class OknoLogin : Form
    {
        public String Domain { get; set; }
        public String Username { get; set; }
        public String Password { get; set; }
        public OknoLogin()
        {
            InitializeComponent();
        }

        private void OknoLogin_Shown(object sender, EventArgs e)
        {
            if (File.Exists("domene.txt"))
            {
                String[] lines = File.ReadAllLines("domene.txt");
                foreach (String line in lines)
                {
                    if (line.Contains(' ')) comboBoxDomain.Items.Add(line.Split(' ')[0]);
                    else comboBoxDomain.Items.Add(line);
                }
                textBoxUsername.Focus();
                if (comboBoxDomain.Items.Count > 0)
                {
                    comboBoxDomain.SelectedIndex = 0;
                }
            }
        }

        private void comboBoxDomain_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (File.Exists("domene.txt"))
            {
                String[] lines = File.ReadAllLines("domene.txt");
                for (int i = 0; i < lines.Length; i++)
                {
                    if (i == comboBoxDomain.SelectedIndex)
                    {
                        if (lines[i].Contains(' '))
                        {
                            textBoxUsername.Text = lines[i].Split(' ')[1];
                            textBoxPassword.Focus();
                        }
                        else
                        {
                            textBoxUsername.Focus();
                        }
                        break;
                    }
                }
            }
        }

        private void buttonPrijava_Click(object sender, EventArgs e)
        {
            Domain = comboBoxDomain.Text;
            Username = textBoxUsername.Text + "@" + Domain;
            Password = textBoxPassword.Text;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void buttonPreklici_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }
    }
}
